﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class UserEdit : System.Web.UI.Page
    {
        protected string id;
        protected User user;
        protected string resultMessage = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"];

            if (!IsPostBack)
            {
                this.lblTitle.Text = "修改用户";
                Users_Dao dao = new Users_Dao();
                user = dao.Query(id);
                setFields();
                setControls();
            }
        }

        private void update()
        {
            user = new User()
            {
                Id = Guid.Parse(id),
            };
            getFields();
            Users_Dao dao = new Users_Dao();

            try
            {
                int result = dao.Update(user);
                if (result == 1)
                {
                    resultMessage = "更新用户成功";
                }
            }
            catch (Exception ex)
            {
                resultMessage = "更新用户失败:" + ex.Message;
            }
        }

        private void setFields()
        {
            this.tbCode.Text = user.Code.Trim();
            this.tbName.Text = user.Name;
            this.rbGender.SelectedIndex = (int)user.Gender;
            this.hdPassword.Value = user.Password;
            this.tbBirthday.Text = user.Birthday.ToString();
            this.tbTel.Text = user.Tel;
            this.tbMobile.Text = user.Mobile;
            this.tbEmail.Text = user.Email;
            this.tbAddress.Text = user.Address;
        }

        private void getFields()
        {
            user.Code = this.tbCode.Text.Trim();
            user.Name = this.tbName.Text.Trim();
            user.Gender = int.Parse(this.rbGender.SelectedValue);
            user.Password = this.hdPassword.Value;
            string dtstr = this.tbBirthday.Text.Trim();
            if (!string.IsNullOrEmpty(dtstr))
            {
                user.Birthday = DateTime.Parse(dtstr);
            }
            user.Tel = this.tbTel.Text.Trim();
            user.Mobile = this.tbMobile.Text.Trim();
            user.Email = this.tbEmail.Text.Trim();
            user.Address = this.tbAddress.Text.Trim();
        }

        private void setControls()
        {
            this.tbBirthday.Attributes.Add("onclick", "show()");
        }

        private void ClearControlsValue()
        {
            Users_Dao dao = new Users_Dao();
            user = dao.Query(id);
            setFields();
            setControls();
            this.lbResult.Text = "";
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            AsyncPostBackTrigger outsideTrigger = new AsyncPostBackTrigger();
            outsideTrigger.ControlID = "btnSave";
            outsideTrigger.EventName = "Click";
            this.UpdatePanel2.Triggers.Add(outsideTrigger);
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            update();

            this.lbResult.Text = resultMessage;
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("UserManage.aspx");
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }
    }
}